//
//  LogisViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/10/16.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "LogisViewController.h"
#import "ChineseString.h"

#define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
#define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
@interface LogisViewController ()<UITableViewDelegate,
UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *indexArray;
@property(nonatomic,strong)NSMutableArray *letterResultArr;
@property (nonatomic, strong) UILabel *sectionTitleView;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSDictionary *dataDic;
@end

@implementation LogisViewController
-(NSDictionary*)dataDic{
    if (!_dataDic) {
        _dataDic=[NSDictionary dictionary];
    }
    return _dataDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"物流名称";
    [self loadViewLogistics];
    [self.view addSubview:[self tableView]];
    self.sectionTitleView = ({
        UILabel *sectionTitleView = [[UILabel alloc] initWithFrame:CGRectMake((APP_WIDTH-100)/2, (APP_HEIGHT-100)/2,100,100)];
        sectionTitleView.textAlignment = NSTextAlignmentCenter;
        sectionTitleView.font = [UIFont boldSystemFontOfSize:60];
        sectionTitleView.textColor = [UIColor blueColor];
        sectionTitleView.backgroundColor = [UIColor whiteColor];
        sectionTitleView.layer.cornerRadius = 6;
        sectionTitleView.layer.borderWidth = 1.f/[UIScreen mainScreen].scale;
        _sectionTitleView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        sectionTitleView;
    });
    [self.navigationController.view addSubview:self.sectionTitleView];
    self.sectionTitleView.hidden = YES;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW,MainH-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.backgroundColor=Gray_Color;
        _tableView.dataSource = self;
    }
    return _tableView;
}
#pragma mark -
#pragma mark - UITableViewDataSource

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.indexArray;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.indexArray objectAtIndex:section];
    return key;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.indexArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.dataDic objectForKey:self.indexArray[section]] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * hjID = [NSString stringWithFormat:@"%ldcell%ld",indexPath.section,indexPath.row];
    UITableViewCell * hjcell = [tableView dequeueReusableCellWithIdentifier:hjID];
    if (!hjcell) {
        hjcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hjID];
    }
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSLog(@"key-----%@",self.indexArray[indexPath.section]);
    hjcell.textLabel.text = [[self.dataDic objectForKey:self.indexArray[indexPath.section]]objectAtIndex:indexPath.row][@"zhcnName"];
    hjcell.textLabel.textColor=TextColor6;
    hjcell.textLabel.font=FontAndStyle12;
    return hjcell;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    [self showSectionTitle:title];
    return index;
}


#pragma mark - private
- (void)timerHandler:(NSTimer *)sender
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:.3 animations:^{
            self.sectionTitleView.alpha = 0;
        } completion:^(BOOL finished) {
            self.sectionTitleView.hidden = YES;
        }];
    });
}

-(void)showSectionTitle:(NSString*)title{
    [self.sectionTitleView setText:title];
    self.sectionTitleView.hidden = NO;
    self.sectionTitleView.alpha = 1;
    [self.timer invalidate];
    self.timer = nil;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerHandler:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}



#pragma mark - 
#pragma mark - UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lab = [UILabel new];
    lab.backgroundColor = [UIColor groupTableViewBackgroundColor];
    lab.text = [self.indexArray objectAtIndex:section];
    lab.textColor =TextColor3;
    lab.font=FontAndStyle14;
    return lab;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 49;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 49.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"---->%@",[[self.dataDic objectForKey:self.indexArray[indexPath.section]]objectAtIndex:indexPath.row][@"zhcnName"]);
    
    if (self.styple==1) {
        self.logistBlock([[self.dataDic objectForKey:self.indexArray[indexPath.section]]objectAtIndex:indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)loadViewLogistics{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    [self doHttpRequestWithApi:@"api/bs/logistics/companies" method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.dataDic=dic[@"data"];
        NSArray *array=[dic[@"data"] allKeys];
        self.indexArray = [ChineseString IndexArray:array];
        [self.tableView reloadData];
        NSLog(@"dic-----%@",self.indexArray);
    }];
    
}

@end
